//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fstream>
#include <iostream>

#include "MakeCPPClass.h"
#include "libraries.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define MAX_SIZE 500000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main (int argc, char** argv) {
  
  String* namefilePrototypes = NULL;
  
  if (argc < 2) {
    
    cout << "Error in the input parameters! Must by like: " << endl;
    cout << "./CreateNewClass <Filename with a prototype of class>";
    cout << endl;
    
    return 0;
    
  }
  else {
    
    for (int i = 1; i < argc; i++) {
      
      if (!strcmp(argv[i], "-p")) {

        cout << "-------------------------------------------------------------------------------------" << endl;
        cout << "MODEL TO USE (Without the lines)"                                                      << endl;
        cout << "-------------------------------------------------------------------------------------" << endl;
        cout << "Name: ClassName (inheritance;inheritance)"                                             << endl;
        cout                                                                                            << endl;
        cout << "+Attribute: AttributeName (Type[@Include;...])"                                        << endl;
        cout << "-Attribute: AttributeName (Type[@Include;...])"                                        << endl;
        cout << "*Attribute: AttributeName (Type[@Include;...])"                                        << endl;
        cout                                                                                            << endl;
        cout << "+Method: MethodName (Type{ParameterName}[@Include];...) = Type"                        << endl;
        cout << "-Method: MethodName (Type{ParameterName}[@Include];...) = Type"                        << endl;
        cout << "*Method: MethodName (Type{ParameterName}[@Include];...) = Type"                        << endl;
        cout << "-------------------------------------------------------------------------------------" << endl;
        
        exit (EXIT_SUCCESS);
        
      }
      
    }
    
    namefilePrototypes = new String(argv[1]);
    
    cout << "-------------------------------------------------------------------------------------" << endl;
    cout << " Filename with a prototype of class : " << argv[1] << endl;
    cout << "-------------------------------------------------------------------------------------" << endl;
    
  }
  
  MakeCPPClass* makeCPPClass = new MakeCPPClass ();
  
  cout << "Loading the data..." << endl;
  
  ifstream file (namefilePrototypes->Out());
  
  if (!file.is_open()) {
    cout << "Error loading data!" << endl;
    return EXIT_FAILURE;
  }
  
  cout << "Creating a classes..." << endl;
  
  char    buffer [MAX_SIZE];
  String  data, line;
  
  data.In("");
  
  while (!file.eof()) {
    
    file.getline(buffer, MAX_SIZE);
    
    line.In(buffer);
    line.ITrim();
    if (!line.Length())
      continue;
    
    if (strstr(buffer, "Name:")) {
      
      if (data.Contain("Name:")) {
        
        if (!makeCPPClass->LoadClassInfo(&data)) 
          cout << "Error loading data of Class!" << endl;
        else if (!makeCPPClass->ConstructCPPClass()) 
          cout << "Error constructing the Class!" << endl;
      }
      
      data.In(buffer);
      
    }
    else data.Add(buffer);
    
    data.Add("\n");
    
  }
  
  if (data.Contain("Name:")) {
    if (!makeCPPClass->LoadClassInfo(&data)) 
      cout << "Error loading data of Class!" << endl;
    else if (!makeCPPClass->ConstructCPPClass()) 
      cout << "Error constructing the Class!" << endl;
  }
  
  file.close();
  
  cout << "Finished..." << endl << "Good Bye" << endl;
  
  if (makeCPPClass)       delete makeCPPClass;
  if (namefilePrototypes) delete namefilePrototypes;

  return EXIT_SUCCESS;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//

